/***************************************************************************
 创建者: 　　hualei
 开始时间: 2020-09-29
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.9.29 类的具体实现代码编写

 ***************************************************************************
 *  LIFT 升降机构指令具体实现                                                                       *
 ***************************************************************************/
#ifndef LIFTMECHANISMINSTRUCTIONLOGIC_H
#define LIFTMECHANISMINSTRUCTIONLOGIC_H

#include <QObject>
#include "instructionEnum.h"
#include "GeneralDefine.h"
#include "liftmechanisminstruction.h"
#include <QTimer>
#include <QObject>
#include "fleetdefine.h"

class ProgramSet;
class ProgramEngine;
class RobotIo;
class McControllerThread;

//enum E_LIFT_STATUS
//{
//    E_LIFT_STATUS_MIDDLE,
//    E_LIFT_STATUS_TOP,
//    E_LIFT_STATUS_BUTTOM,
//    E_LIFT_STATUS_MOVINGUP,
//    E_LIFT_STATUS_MOVINGDOWN,
//    E_LIFT_STATUS_ERROR
//};

//struct LiftMechanismConfig
//{
//    int mechanicalType;
//    int heavyLiftDriverType;
//    double heavyLiftSpeed;
//    double heavyLift_axisId;
//    bool isSetSheftRegion;
//    int heavyTopSensor_index;
//    int heavyButtomSensor_index;
//    int heavyMotorPositive_index;
//    int heavyMotorReverse_index;
//    bool isWatchButtonDi;
//    int upButtonDiIndex;
//    int downButtonDiIndex;
//    double liftManualSpeed;
//};

/**
 * @brief The LiftMechanismInstructionLogic class       LIFT 升降机构指令具体实现类
 */
class LiftMechanismInstructionLogic:public QObject
{

    Q_OBJECT

public:

    /**
     * @brief LiftMechanismInstructionLogic      构造函数，初始成员变量
     * @param ioControllerIn                     io控制器对象
     * @param robotIdIn                           机器人编号
     * @param programSetIn                        程序指令集合对象
     * @param programEngineIn                   　程序解释引擎对象
     */
    LiftMechanismInstructionLogic(RobotIo* ioControllerIn, int robotIdIn, ProgramSet* programSetIn,
                                  ProgramEngine* programEngineIn,McControllerThread *programMcControllerIn);

    int stop();
    int clearAllMsg( );
    E_LIFT_STATUS getLiftMechanismStatus();
    int setLiftMechanismConfig(LiftMechanismConfig configIn);
    int getLiftMechanismConfig(LiftMechanismConfig &configOut);
    /**
     * @brief startLiftMechanism                 开始执行升降机构指令
     * @param programNumIn                       程序编号
     * @param instructionIn                      升降机构指令对象
     * @param lineIndex                          当前指令行索引值
     * @return
     */
    E_PROGRAM_LOOP_RESULT startLiftMechanism(int programNumIn,const LiftMechanismInstruction& instructionIn, int lineIndex);

    E_PROGRAM_LOOP_RESULT liftMechanism_run(const LiftMechanismInstruction& instructionIn);
    E_PROGRAM_LOOP_RESULT liftMechanism_run(const LiftParameter& paraIn);



    int setJogButtonEnable(bool isEnableIn);//以前自动模式禁止手动，聿冠要求自动下也可以按下来。

    //与roller公用
    E_PROGRAM_LOOP_RESULT blockMechanismUpDown(EM_LIFT_TYPE moveTypeIn, E_BLOCK_TYPE blokType, int motorDo1Index,
                             int motorDo2Index, int topArrivalIndex, int buttomArrivalIndex, int middleArrivalIndex,
                     E_BLOCK_TYPE blokTypeB, int motorDo1IndexB,
                    int motorDo2IndexB, int topArrivalIndexB, int buttomArrivalIndexB,
                             int middleArrivalIndexB, double blockWaitTime,bool isExchangeDirectionIn=false);

private:
    void judgeCurrentStatus();
    int readConfigFile(QString fileName, QString &readComent);
    int writeConfigFile(QString fileName,LiftMechanismConfig configIn);

    void liftUp(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index,bool isExchangeDirectionIn);
    void liftDown(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index,bool isExchangeDirectionIn);
    void liftStop(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index);

    bool isInTop_block(int topArrivalIndex,int buttomArrivalIndex);
    bool isInButtom_block(int topArrivalIndex,int buttomArrivalIndex);
    bool isInMiddle_block(int middleArrivalIndex,int buttomArrivalIndex,int topArrivalIndex);


//    bool isInTop();
//    bool isInButtom();
//    bool isInMiddle();


    int heavyLiftUp_netDriver(int axisIdIn);
    int heavyLiftDown_netDriver(int axisIdIn);
    int heavyLiftStop_netDriver(int axisIdIn);

//    int commonLiftUp();
//    int commonLiftDown();
//    int commonLiftStop();
//    int heavyLiftUp();
//    int heavyLiftDown();
//    int heavyLiftStop();
//    int heavyLiftUp_do();
//    int heavyLiftDown_do();
//    int heavyLiftStop_do();

    void realTimeSleep(long long umicoroSecond);//最大定时睡去３Ｓ，超过就溢。
    /**
     * @brief addMsg                添加消息
     * @param messageLevel          消息类别
     * @param componentName
     * @param messageType
     * @param messageCode
     * @param robotId
     * @param parameter1
     * @param parameter2
     * @param parameter3
     * @param parameter4
     * @param message
     */
    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0,
                 QString message= "");

    void* threadRun(void *);
    static void* threadTask( void* classPtr );
    void createThread();
    int jogButtonMonitor();
    int monitorCargo();


private:
    LiftMechanismConfig mechanismConfig;
    QString configFilePath;
    E_LIFT_STATUS basicStatus;

    /**
     * @brief robotId           机器人编号
     */
    int robotId;

    /**
     * @brief programSet        指令程序集合
     */
    ProgramSet *programSet;

    /**
     * @brief programEngine     程序解释引擎
     */
    ProgramEngine *programEngine;

    /**
     * @brief ioControllerIn    io控制对象
     */
    RobotIo *ioController;

    /**
     * @brief timer1            定时器
     */
    QTimer timer;

    /**
     * @brief liftTimer         升降机构定时器
     */
    QTimer  liftTimer;

    /**
     * @brief programNum        程序编号
     */
    int programNum;

    /**
     * @brief jmpLbNum          异常跳转标签号
     */
    int jmpLbNum;
    McControllerThread *mcController;
    pthread_t threadId;
    bool isInitialStatus;


//    /**
//     * @brief currentLiftType       当前升降类型
//     */
//    EM_LIFT_TYPE currentLiftType;

    bool isButtonDiValid;//手动按钮是否有效

//    int heavyLiftDriverType;//举升驱动类型 １　开观点控制　２网络驱动器速度控制
//    double heavyLiftSpeed;网络驱动器举升速度
//    int heavyLift_axisId;网络驱动器轴号
//    int commonTopSensor_index;//从０开始
//    int commonButtomSensor_index;
//    int commonMotorDo_index;//从０开始
//    int heavyTopSensor_index;//从０开始
//    int heavyButtomSensor_index;
//    int heavyMotorPositive_index;
//    int heavyMotorReverse_index;////从０开始
//    bool isWatchButtonDi;//是否启动监视手动按钮
//    int upButtonDiIndex;//手动上di
//    int downButtonDiIndex;//手动下di
//    int liftManualSpeed;//手动速度
//    bool isSetSheftRegion;//是否设置料架壁障区域

    bool isRuned;
    bool sensorIsNormalOn;
    int cargoSensorDiIndex;//叉车去监视货物存在检测。不使用则设置为负数
    bool isInCargoMonitor;



};

#endif // LIFTMECHANISMINSTRUCTIONLOGIC_H
